import java.util.*;

public class Airport {

	ReadAndWrite rw = new ReadAndWrite(this);
	private List<Runway> runways = null;

	private int M;
	private int N;
	private int T;
	private int planesOnGround;
	private int moment;
	private Plane noPlane = new Plane(0, 0);

	public Airport() {
		runways = new ArrayList<>(M);
	}

	public void closeRunway(int runwayNumber, int timeBeingClosedFor) {
		runways.get(runwayNumber).setIsClosed(true);
		runways.get(runwayNumber).setClosedFor(timeBeingClosedFor);
	}

	public void requestsLanding(Plane plane) {
		if (!checkCapacity()) {
			redirectToTheWoods(plane);
			return;
		}

		for (int i = 0; i < runways.size(); i++) {
			if (runways.get(i).isBusy()) {
				if (runways.get(i).getPlane().getID() == plane.getID()) {
					rw.write(String.format("%d has already landed",
							plane.getID()));
					return;
				}
			}

		}

		for (int i = 0; i < runways.size(); i++) {

			if (!runways.get(i).isBusy() && !runways.get(i).isClosed()) {

				if (plane.getHasFuelFor() >= T) {
					landing(plane, i);
					break;
				} else {
					redirectToTheWoods(plane);
					break;
				}
			}
		}
	}

	private void landing(Plane plane, int numberOfRunaway) {
		runways.get(numberOfRunaway).setIsBusy(true);
		runways.get(numberOfRunaway).setPlane(plane);
		rw.write(String.format("%d lands on %d", plane.getID(),
				numberOfRunaway + 1));
		planesOnGround++;
	}

	private boolean checkCapacity() {
		return getPlanesOnGround() < getN() && getPlanesOnGround() < getM() ? true
				: false;
	}

	public void requestsTakingOff(Plane plane) {

		if (runways.size() == 0) {
			rw.write("no plane to take off");
			return;
		}

		boolean hasSuchPlane = false;

		for (int i = 0; i < runways.size(); i++) {
			if (runways.get(i).getPlane().getID() == plane.getID()) {
				takingOff(plane, i);
				hasSuchPlane = true;
				return;
			}
		}
		if (!hasSuchPlane) {
			rw.write(String.format("%d hasn't landed on this airport!",
					plane.getID()));
		}
	}

	public void takingOff(Plane plane, int numberOfRunaway) {
		runways.get(numberOfRunaway).setIsBusy(false);
		runways.get(numberOfRunaway).setPlane(noPlane);
		rw.write(String.format("%d takes off from %d", plane.getID(),
				numberOfRunaway + 1));
		planesOnGround--;

	}

	public void redirectToTheWoods(Plane plane) {
		rw.write(String.format("%d redirected", plane.getID()));
	}

	public void update() {
		for (int i = 0; i < runways.size(); i++) {
			runways.get(i).update();
		}
	}

	public int getPlanesOnGround() {
		return planesOnGround;
	}

	public void setPlanesOnGround(int planesOnGround) {
		this.planesOnGround = planesOnGround;
	}

	public void openForBuisness() {

		for (int i = 0; i <= M; i++) {
			runways.add(new Runway(noPlane));
		}
		rw.read();
	}

	public int getMoment() {
		return moment;
	}

	public void setMoment(int moment) {
		this.moment = moment;
	}

	public void incrementMoment() {
		moment++;
		update();
	}

	public int getM() {
		return M;
	}

	public void setM(int m) {
		M = m;
	}

	public int getN() {
		return N;
	}

	public void setN(int n) {
		N = n;
	}

	public int getT() {
		return T;
	}

	public void setT(int t) {
		T = t;
	}

}
